package common.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.text.TextUtils;

public class ContactUtil {
	/**
	 * 
	 * @param context
	 * @param phoneNumber
	 * @return
	 * 电话本里有，返回名字，否则返回电话号码
	 */
	public static String getContactName(Context context,String phoneNumber){
		if (TextUtils.isEmpty(phoneNumber)) {
			return "";
		}
		String name = "";
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
		Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
		if (cursor!=null) {
			if (cursor.moveToFirst()) {
				//是联系人，用名字
				name = cursor.getString(0);
			}
			cursor.close();
		}

		//不是联系人，直接用号码
//		if (name==null) {
//			name = phoneNumber;
//		}
		return  name;
	}
}
